﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
using System.Resources;
using System.Threading;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Marketplace;

namespace CarBloke.Pages
{
    public partial class SelectVehiclePage : PhoneApplicationPage
    {
        private Boolean isExpired = false;

        public SelectVehiclePage()
        {
            InitializeComponent();
            PopulateVehicles();

            //Application bar and application menu text must be set here explicitly.
            var newButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            var settingsMenuItem = ApplicationBar.MenuItems[0] as ApplicationBarMenuItem;
            newButton.Text = LanguageResources.Resources._new;
            settingsMenuItem.Text = LanguageResources.Resources._options;

            LicenseInformation licenseInfo = new LicenseInformation();
            if (licenseInfo.IsTrial())
            {
                PurchaseFullButton.Visibility = System.Windows.Visibility.Visible;
                TrailExpirationLabel.Visibility = System.Windows.Visibility.Visible;

                int daysLeft = App.GetRemainingTrialDays();
                if (daysLeft >= 0)
                {
                    TrailExpirationLabel.Text = daysLeft.ToString(CultureInfo.CurrentCulture) + " " + LanguageResources.Resources._daysleft;
                }
                else
                {
                    TrailExpirationLabel.Text = "";
                    ExpiredCanvas.Visibility = System.Windows.Visibility.Visible;
                    isExpired = true;
                }
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            PopulateVehicles();
        }

        private void PopulateVehicles()
        {
            LogEntryListBox.Items.Clear();
            foreach (Vehicle vehicle in App.LoadVehicles())
            {
                LogEntryListBox.Items.Add(vehicle);
            }
        }

        private void appbar_newButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(PageRegistry.VehicleEditPage, UriKind.RelativeOrAbsolute));
        }

        private void LogEntryListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (LogEntryListBox.SelectedIndex == -1)
                return;

            Vehicle vehicle = LogEntryListBox.SelectedItem as Vehicle;

            if (vehicle != null && !isExpired)
            {
                NavigationService.Navigate(new Uri(PageRegistry.VehiclePage +"?vehicleID=" + vehicle.ID, UriKind.RelativeOrAbsolute));
            }

            // Reset selected index to -1 (no selection)
            LogEntryListBox.SelectedIndex = -1;
        }


        private void settingsMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(PageRegistry.SettingsPage, UriKind.RelativeOrAbsolute));
        }

        private void ItemButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isExpired)
            {
                Button button = sender as Button;
                Guid vehicleID = (Guid)button.Tag;

                NavigationService.Navigate(new Uri(PageRegistry.LogEditPage
                    + "?vehicleID=" + vehicleID.ToString()
                    + "&logType=" + LogEntry.LogTypes.Fuel.ToString(),
                    UriKind.RelativeOrAbsolute));
            }
        }

        private void PurchaseFullButton_Click(object sender, RoutedEventArgs e)
        {
            MarketplaceDetailTask updateTask = new MarketplaceDetailTask();
            updateTask.Show();
        }
    }
}